import socket
import struct


def connect_env():
    global sc
    sc = socket.socket()
    sc.connect(('127.0.0.1', 7777))
    print('已连接到环境')


def send_to_env(x):
    bytes_obj = struct.pack('f' * len(x), *x)
    sc.sendall(bytes_obj)
    print('向环境发送数据', x)


def rec_from_env():
    bytes_obj = sc.recv(1024)
    n = int(len(bytes_obj) / 4)
    s = struct.unpack('f' * n, bytes_obj)
    print('从环境接收数据', s)
    return s

